package com.topcent.tioxyc.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.topcent.tioxyc.constants.KeyConstant;
import com.topcent.tioxyc.pojo.JdyPurchaseOrder;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: 向伟典
 * @date: 2023/9/6 13:54
 * @Description: 公有的方法
 */
@Slf4j
public class PurchaseCommunity {


    /**
     * 根据code查询简道云数据
     */
    public static List<JdyPurchaseOrder> queryBased(Map<String, Object> parameter, Header[] headers, String voucherCode, String fieldName){
        Map<String ,Object> filter=new HashMap<>();
        filter.put("rel","and");
        Map<String ,Object> cond =new HashMap<>();
        cond.put("field",fieldName);
        cond.put("type","string");
        cond.put("method","eq");
        List<String> values =new ArrayList<>();
        values.add(voucherCode);
        cond.put("value",values);
        List<Map<String,Object>> conds=new ArrayList<>();
        conds.add(cond);
        filter.put("cond",conds);
        parameter.put("filter",filter);
        String jsonSale = JSONUtil.toJsonStr(parameter);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
        String data = jdySale.getStr("data");
        JSONArray objects = JSONUtil.parseArray(data);
        return JSONUtil.toList(objects, JdyPurchaseOrder.class);
    }
    public static <T> List<T> queryBased( Map<String, Object> parameter,Header[] headers,String voucherCode,String fieldName,Class<T> clazz){
        Map<String ,Object> filter=new HashMap<>();
        filter.put("rel","and");
        Map<String ,Object> cond =new HashMap<>();
        cond.put("field",fieldName);
        cond.put("type","string");
        cond.put("method","eq");
        List<String> values =new ArrayList<>();
        values.add(voucherCode);
        cond.put("value",values);
        List<Map<String,Object>> conds=new ArrayList<>();
        conds.add(cond);
        filter.put("cond",conds);
        parameter.put("filter",filter);
        String jsonSale = JSONUtil.toJsonStr(parameter);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
        String data = jdySale.getStr("data");
        JSONArray objects = JSONUtil.parseArray(data);
        return JSONUtil.toList(objects, clazz);
    }
    public static <T> List<T> queryBased( Map<String, Object> parameter,Header[] headers,HashMap<String,String> map,Class<T> clazz){
        Map<String ,Object> filter=new HashMap<>();
        filter.put("rel","and");
        List<Map<String,Object>> conds=new ArrayList<>();
        map.forEach((key,value) ->{
            Map<String ,Object> cond =new HashMap<>();
            cond.put("field",key);
            cond.put("type","string");
            cond.put("method","eq");
            List<String> values =new ArrayList<>();
            values.add(value);
            cond.put("value",values);
            conds.add(cond);
        });
        filter.put("cond",conds);
        parameter.put("filter",filter);
        String jsonSale = JSONUtil.toJsonStr(parameter);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
        String data = jdySale.getStr("data");
        JSONArray objects = JSONUtil.parseArray(data);
        return JSONUtil.toList(objects, clazz);
    }

    /**
     * 带时间参数的查询方法类
     * @param parameter 参数集合
     * @param time 时间区间
     * @param headers 头部
     * @param map
     * @param clazz
     * @return
     * @param <T>
     */
    public static <T> List<T> queryBased(Map<String, Object> parameter, Map<String, LocalDateTime> time, Header[] headers, HashMap<String,String> map, Class<T> clazz){
        Map<String ,Object> filter=new HashMap<>();
        filter.put("rel","and");
        List<Map<String,Object>> conds=new ArrayList<>();
        map.forEach((key,value) ->{
            Map<String ,Object> cond =new HashMap<>();
            cond.put("field",key);
            cond.put("type","string");
            cond.put("method","eq");
            List<String> values =new ArrayList<>();
            values.add(value);
            cond.put("value",values);
            conds.add(cond);
        });
        AtomicInteger i= new AtomicInteger(0);
        time.forEach((key,value) ->{
            Map<String ,Object> cond =new HashMap<>();
            cond.put("field",key);
            cond.put("method","range");
            List<Object> values =new ArrayList<>();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            if (i.get()==0){
                values.add(null);
                values.add(value.format(formatter));
            }else {
                values.add(value.format(formatter));
                values.add(null);
            }
            cond.put("value",values);
            conds.add(cond);
            i.set(1);
        });

        filter.put("cond",conds);
        parameter.put("filter",filter);
        // 配置 JSONConfig，保留 null 值
        JSONConfig config = new JSONConfig();
        config.setIgnoreNullValue(false);  // 允许 null 值显示
        String jsonSale = JSONUtil.toJsonStr(parameter,config);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
        String data = jdySale.getStr("data");
        JSONArray objects = JSONUtil.parseArray(data);
        return JSONUtil.toList(objects, clazz);
    }
}
